﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace TFSLOC
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                PrintUsageInformation();
                System.Environment.Exit(1);
            }

            string rootPath = args[0];
            string beforePath = rootPath + @"\Before";
            string afterPath = rootPath + @"\After";

            List<Int32> changesetIDs = new List<int>();
            for (int i = 1; i < args.Length; i++)
            {
                changesetIDs.Add(Int32.Parse(args[i]));
            }

            TeamFoundationServer TFServer = null;
            VersionControlServer VCServer = null;

            try
            {
                string[] serverNames = RegisteredServers.GetServerNames();
                foreach (string name in serverNames)
                {
                    try
                    {
                        TFServer = TeamFoundationServerFactory.GetServer(name);
                        VCServer = (VersionControlServer)TFServer.GetService(typeof(VersionControlServer));
                        break;
                    }
                    catch (Exception) { }
                }

                try
                {

                    foreach (Int32 changesetID in changesetIDs)
                    {
                        Changeset changeset = VCServer.GetChangeset(changesetID, true, true);

                        foreach (Change change in changeset.Changes)
                        {
                            string localAfterPath = afterPath + @"\C" + changesetID.ToString() + change.Item.ServerItem.Substring(1);

                            if (change.ChangeType != ChangeType.Delete)
                            {
                                change.Item.DownloadFile(localAfterPath);
                            }

                            Item beforeItem = VCServer.GetItem(change.Item.ItemId, changesetID - 1);
                            if (beforeItem != null)
                            {
                                string localBeforePath = beforePath + @"\C" + changesetID.ToString() + change.Item.ServerItem.Substring(1);
                                beforeItem.DownloadFile(localBeforePath);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Environment.Exit(3);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Environment.Exit(2);
            }

            Environment.Exit(0);
        }

        static protected void PrintUsageInformation()
        {
            Console.WriteLine("Usage: TFSLOC <Root Dump Path> <Changeset List>");
        }
    }
}
